using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WP.Modules;
using System.Collections;
using WP.DB;
using WP.Commons;
namespace MaxLeaf
{
    public partial class frmLanguageSetting : BaseQryRpt 
    {
        public frmLanguageSetting()
        {
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));
        }

        private void frmLanguageSetting_Load(object sender, EventArgs e)
        {
            DBAccess dbcon = new DBAccess(false, false);
            DataView dv = dbcon.GetDataTable("select * from mLanguage").DefaultView;
            foreach (DataRowView drw in dv)
            {
                GetControl("txtLanguage" + drw["LanguageSeqNo"].ToString()).Text = drw["Language"].ToString().Trim() ;
            }

            txtLanguage1.Text = "English";
            dbcon.CloseCon();  
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!Valid()) return;
            DBAccess dbcon = new DBAccess(false, false);
            string strSql = "delete from mLanguage";
            dbcon.ExecuteSql(strSql);
            for (int i = 1; i <= 6; i++)
            {
                Control con = GetControl("txtLanguage" + i.ToString().Trim() );
                if (con != null)
                {
                    string strText = con.Text.Trim();
                    if (strText != "")
                    {
                        strSql = "Insert into mLanguage (Language,Description,LanguageSeqNo) values ('" + strText + "',''," + i.ToString() + ")";
                        dbcon.ExecuteSql(strSql);
                    }
                }
            }
            dbcon.CloseCon();  
            this.Close(); 
        }


        private bool Valid()
        {
            string strLanguage = "";
            if (txtLanguage1.Text.Trim() != "") strLanguage += txtLanguage1.Text.Trim().ToUpper() + ";";
            if (txtLanguage2.Text.Trim() != "") strLanguage += txtLanguage2.Text.Trim().ToUpper() + ";";
            if (txtLanguage3.Text.Trim() != "") strLanguage += txtLanguage3.Text.Trim().ToUpper() + ";";
            if (txtLanguage4.Text.Trim() != "") strLanguage += txtLanguage4.Text.Trim().ToUpper() + ";";
            if (txtLanguage5.Text.Trim() != "") strLanguage += txtLanguage5.Text.Trim().ToUpper() + ";";
            if (txtLanguage6.Text.Trim() != "") strLanguage += txtLanguage6.Text.Trim().ToUpper() + ";";
            if (strLanguage.Length == 0)
            {
                MsgBoxEx.Show(Msg.Select("Please enter language.", DataFunctions.GetMsg(this.Name, "MSG_PleaseEnterLanguage")), SysVar.WarnTitle);
                return false;
            }
            else
            {
                strLanguage = strLanguage.Substring(0, strLanguage.Length - 1); 
            }
            string [] arrLanguage = strLanguage.Split(new char[] { ';' });

            for (int i = 0; i < arrLanguage.Length; i++)
            {
                for (int j = 0; j < arrLanguage.Length; j++)
                {
                    if (i != j && arrLanguage[i]==arrLanguage[j] )
                    {
                        MsgBoxEx.Show(Msg.Select("Language can not be duplicated.", DataFunctions.GetMsg(this.Name, "MSG_LanguageCanNotBeDuplicated")), SysVar.WarnTitle);
                        return false;
                    }
                }
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }

    }
}